<template>
  <div>
    <el-row type="flex" align="middle" style="padding: 5px 0px">
      <el-col :span="10">
        <div>数据模型</div>
      </el-col>
      <el-col :span="4">
        <el-button
          @click="dialogVisible = true"
          type="primary"
          icon="el-icon-edit"
          circle
        ></el-button>
      </el-col>
    </el-row>

    <el-dialog
      width="60%"
      title="数据模型对象"
      destroy-on-close
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="dialogVisible"
      append-to-body
    >
      <editor
        v-model="eventContent"
        @init="editorInit"
        :options="editorOptions"
        lang="json"
        theme="chrome"
        width="100%"
        height="350"
      ></editor>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveModels()">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["store"],
  components: {
    editor: require("vue2-ace-editor"),
  },
  data() {
    return {
      editorOptions: {
        enableBasicAutocompletion: true, // 启用基本自动完成
        enableSnippets: true, // 启用代码段
        enableLiveAutocompletion: true, // 启用实时自动完成
        printMarginColumn: 30,
        displayIndentGuides: false, // 显示参考线
        enableEmmet: true, // 启用Emmet
        tabSize: 5, // 标签大小
        fontSize: 12, // 设置字号
        useWorker: true, // 使用辅助对象
        showPrintMargin: false, //去除编辑器里的竖线
        enableMultiselect: true, //     选中多处
        readOnly: false, // 是否只读
        showFoldWidgets: true, // 显示折叠部件
        fadeFoldWidgets: true, // 淡入折叠部件
        wrap: true, //换行
      },
      dialogVisible: false,
      eventContent: JSON.stringify(this.store.models, null, 2),
    };
  },
  methods: {
    editorInit: function () {
      require("brace/ext/language_tools"); //language extension prerequsite...
      require("brace/theme/chrome");
      require("brace/mode/javascript"); //language
      require("brace/snippets/javascript"); //snippet
      require("brace/mode/json"); //language
      require("brace/snippets/json"); //snippet
    },
    random() {
      let len = 8;
      let $chars = "ABCDEFGHJKMNPQRSTWXYZ";
      let maxPos = $chars.length;
      let id = "";
      for (let i = 0; i < len; i++) {
        id += $chars.charAt(Math.floor(Math.random() * maxPos));
      }
      return id;
    },
    saveModels() {
      this.store.models = JSON.parse(this.eventContent);
      this.store.formModel.buildModelsList();
      this.store.initComponentTree();
      this.dialogVisible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.form-config,
.box-model-config {
  padding-left: 10px;
}
.form-config .title,
.box-model-config .title {
  line-height: 32px;
  font-size: 14px;
}
.form-config .el-input-number,
.box-model-config .el-input-number {
  line-height: 30px;
}

.help-icon-wrap {
  display: flex;
  justify-items: center;
  cursor: pointer;
  img {
    width: 30px;
    height: 30px;
    margin-top: 5px;
  }
  span {
    line-height: 39px;
  }
}
</style>